package thread.lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 锁的可重入性
 *
 * @author liweimin
 * @date 2021/5/7 10:59
 * @since
 */
public class Test01 {
    public static void main(String[] args) {
        Test01 obj = new Test01();
        new Thread(obj::method1).start();//开启线程调用method1,
    }

    public synchronized void method1() {
        System.out.println("同步方法1");
        //执行method1方法，调用method2方法时，未释放锁
        method2();
    }

    private synchronized void method2() {
        System.out.println("同步方法2");
        method3();
    }

    private void method3() {

        System.out.println("同步方法3");
    }

    static class SubThread implements Runnable {
        private static final ReentrantLock lock = new ReentrantLock();

        @Override
        public void run() {
            try {
                lock.lock();//加两次锁
                lock.lock();
                for (int i = 0; i < 1000; i++) {
                    System.out.println(Thread.currentThread().getName() + "==>" + i);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();//解锁两次
                lock.unlock();
            }
        }
    }
}
